from django.db import models
import re

from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError


def validate_mobile(value):
    """
        使用正则表达式校验手机号格式
        """
    if not re.match(r'^(13|14|15|17|18|19)\d{9}$', value):
        raise ValidationError(message='手机号格式不正确，应为11位数字并以13、14、15、17、18或19开头')


# Create your models here.
class User(AbstractUser):
    SEX_CHOICES = [('1', '男'), ('2', '女')]
    mobile = models.CharField(help_text='手机号', max_length=11, unique=True, verbose_name="手机号",
                              validators=[validate_mobile])
    sex = models.CharField(help_text='性别', max_length=1, verbose_name="性别", choices=SEX_CHOICES, default='1')

    class Meta:
        db_table = 'tb_user'
        verbose_name = 'user'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

    REQUIRED_FIELDS = ['mobile']
